﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using ServiceContracts;

namespace Presention
{
    public partial class CreateNewAuthority : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ServiceAgent proxy = new ServiceAgent())
                {
                    this.Level.DataSource = proxy.ServiceClient.GetAllAuthorityLevel();
                    this.Level.DataTextField = "LevelName";
                    this.Level.DataValueField = "LevelId";
                    this.Level.DataBind();
                    Level.Items.Insert(0, new ListItem("", ""));
                }
            }
        }

        protected void NewButton_Click(object sender, EventArgs e)
        {
                using (ServiceAgent proxy = new ServiceAgent())
                {
                    string newAuthorityName = AuthorityName.Text;
                    string levelName = this.Level.SelectedItem.Text;
                    AuthorityDTO authorityDTO = new AuthorityDTO();
                    AuthorityLevelDTO authorityLevelDTO = new AuthorityLevelDTO();
                    authorityDTO.Level = authorityLevelDTO;
                    authorityDTO.AuthorityName = newAuthorityName;
                    authorityDTO.Level.LevelId = int.Parse(Level.SelectedValue);
                    authorityDTO.Level.LevelName = Level.SelectedItem.Text;
                    proxy.ServiceClient.AddNewAuthority(authorityDTO);
                    Response.Redirect("AuthorityList.aspx");
                }
        }
    }
}